package com.kds.smarthome.controller;

import com.kds.smarthome.common.AjaxResponse;
import com.kds.smarthome.entity.EnergyStatistics;
import com.kds.smarthome.service.EnergyStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/energy-statistics")
public class EnergyStatisticsController {

    @Autowired
    private EnergyStatisticsService energyStatisticsService;

    /**
     * 获取所有能源统计项目
     */
    @GetMapping
    @PreAuthorize("hasRole('ADMIN')")
    public AjaxResponse<List<EnergyStatistics>> getAllEnergyStatistics() {
        List<EnergyStatistics> statisticsList = energyStatisticsService.findAll();
        return AjaxResponse.success(statisticsList);
    }

    /**
     * 根据ID获取能源统计项目
     */
    @GetMapping("/{tid}")
    public AjaxResponse<EnergyStatistics> getEnergyStatisticsById(@PathVariable Long tid) {
        EnergyStatistics statistics = energyStatisticsService.findById(tid);
        if (statistics == null) {
            return AjaxResponse.error("统计项目不存在");
        }
        return AjaxResponse.success(statistics);
    }

    /**
     * 根据建筑编码获取能源统计项目列表
     */
    @GetMapping("/building/{buildingCode}")
    public AjaxResponse<List<EnergyStatistics>> getEnergyStatisticsByBuilding(@PathVariable String buildingCode) {
        List<EnergyStatistics> statisticsList = energyStatisticsService.findByBuildingCode(buildingCode);
        return AjaxResponse.success(statisticsList);
    }

    /**
     * 根据建筑编码和统计项目编码获取能源统计项目
     */
    @GetMapping("/building/{buildingCode}/item/{itemCode}")
    public AjaxResponse<EnergyStatistics> getEnergyStatisticsByBuildingAndItemCode(
            @PathVariable String buildingCode,
            @PathVariable String itemCode) {
        EnergyStatistics statistics = energyStatisticsService.findByBuildingCodeAndItemCode(buildingCode, itemCode);
        if (statistics == null) {
            return AjaxResponse.error("统计项目不存在");
        }
        return AjaxResponse.success(statistics);
    }

    /**
     * 创建能源统计项目
     */
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public AjaxResponse<?> createEnergyStatistics(@RequestBody EnergyStatistics energyStatistics) {
        try {
            energyStatisticsService.createEnergyStatistics(energyStatistics);
            return AjaxResponse.success("创建能源统计项目成功");
        } catch (Exception e) {
            log.error("创建能源统计项目失败", e);
            return AjaxResponse.error(e.getMessage());
        }
    }

    /**
     * 更新能源统计项目
     */
    @PutMapping("/{tid}")
    @PreAuthorize("hasRole('ADMIN')")
    public AjaxResponse<?> updateEnergyStatistics(
            @PathVariable Long tid,
            @RequestBody EnergyStatistics energyStatistics) {
        try {
            energyStatistics.setTid(tid);
            energyStatisticsService.updateEnergyStatistics(energyStatistics);
            return AjaxResponse.success("更新能源统计项目成功");
        } catch (Exception e) {
            log.error("更新能源统计项目失败", e);
            return AjaxResponse.error(e.getMessage());
        }
    }

    /**
     * 删除能源统计项目
     */
    @DeleteMapping("/{tid}")
    @PreAuthorize("hasRole('ADMIN')")
    public AjaxResponse<?> deleteEnergyStatistics(@PathVariable Long tid) {
        try {
            energyStatisticsService.deleteEnergyStatistics(tid);
            return AjaxResponse.success("删除能源统计项目成功");
        } catch (Exception e) {
            log.error("删除能源统计项目失败", e);
            return AjaxResponse.error(e.getMessage());
        }
    }

    /**
     * 删除建筑下所有能源统计项目
     */
    @DeleteMapping("/building/{buildingCode}")
    @PreAuthorize("hasRole('ADMIN')")
    public AjaxResponse<?> deleteEnergyStatisticsByBuilding(@PathVariable String buildingCode) {
        try {
            energyStatisticsService.deleteByBuildingCode(buildingCode);
            return AjaxResponse.success("删除建筑下所有能源统计项目成功");
        } catch (Exception e) {
            log.error("删除建筑下所有能源统计项目失败", e);
            return AjaxResponse.error(e.getMessage());
        }
    }

    /**
     * 删除建筑下指定编码的能源统计项目
     */
    @DeleteMapping("/building/{buildingCode}/item/{itemCode}")
    @PreAuthorize("hasRole('ADMIN')")
    public AjaxResponse<?> deleteEnergyStatisticsByBuildingAndItemCode(
            @PathVariable String buildingCode,
            @PathVariable String itemCode) {
        try {
            energyStatisticsService.deleteByBuildingCodeAndItemCode(buildingCode, itemCode);
            return AjaxResponse.success("删除指定能源统计项目成功");
        } catch (Exception e) {
            log.error("删除指定能源统计项目失败", e);
            return AjaxResponse.error(e.getMessage());
        }
    }
} 